package is.xdumper.xml;

import is.bean.entity.department.Department;
import is.bean.entity.employee.Employee;
import is.bean.entity.region.Region;
import is.xdumper.XdumperConf;
import is.xdumper.type.Dump;
import java.io.*;
import java.util.Iterator;

/**
 *
 * @author aspr0
 */
public class XdumperXmlMarshaller
{
    /**
     * String.format: department, employee, region
     */
    private static final String ROOT_CANVAS =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<?xml-stylesheet type=\"text/xsl\" href=\""+XdumperConf.PATH_DUMP_XSLT_RELATIVE+"\"?>"
            + "<dump dumpReferences=\"%s\" majorEntity=\"%s\">"
            + "<department>"
            + "%s"
            + "</department>"
            + "<employee>"
            + "%s"
            + "</employee>"
            + "<region>"
            + "%s"
            + "</region>"
            + "</dump>";
    /**
     * String.format: id, name, parentId, bossId
     */
    private static final String ROW_DEPARTMENT_CANVAS =
            "<row>"
            + "<id>%s</id>"
            + "<name>%s</name>"
            + "<parentId>%s</parentId>"
            + "<regionId>%s</regionId>"
            + "<bossId>%s</bossId>"
            + "</row>";
    /**
     * String.format: id, lastName, middleName, departmentId
     */
    private static final String ROW_EMPLOYEE_CANVAS =
            "<row>"
            + "<id>%s</id>"
            + "<lastName>%s</lastName>"
            + "<firstName>%s</firstName>"
            + "<middleName>%s</middleName>"
            + "<departmentId>%s</departmentId>"
            + "</row>";
    /**
     * String.format: id, name, parentId
     */
    private static final String ROW_REGION_CANVAS =
            "<row>"
            + "<id>%s</id>"
            + "<name>%s</name>"
            + "<parentId>%s</parentId>"
            + "</row>";

    public static void marshalDump(Dump dump, File xml) throws IOException
    {
        Iterator<Department> departments = dump.getDepartmentTable().iterator();
        String rowsDepartment = "";
        while (departments.hasNext())
        {
            Department department = departments.next();
            String rowDepartment = String.format(ROW_DEPARTMENT_CANVAS,
                                                 department.getId(),
                                                 department.getName(),
                                                 department.getParentId(),
                                                 department.getRegionId(),
                                                 department.getBossId());
            rowsDepartment += rowDepartment;
        }

        Iterator<Employee> employees = dump.getEmployeeTable().iterator();
        String rowsEmployee = "";
        while (employees.hasNext())
        {
            Employee employee = employees.next();
            String rowEmployee = String.format(ROW_EMPLOYEE_CANVAS,
                                               employee.getId(),
                                               employee.getLastName(),
                                               employee.getFirstName(),
                                               employee.getMiddleName(),
                                               employee.getDepartmentId());
            rowsEmployee += rowEmployee;
        }

        Iterator<Region> regions = dump.getRegionTable().iterator();
        String rowsRegion = "";
        while (regions.hasNext())
        {
            Region region = regions.next();
            String rowRegion = String.format(ROW_REGION_CANVAS,
                                             region.getId(),
                                             region.getName(),
                                             region.getParentId());
            rowsRegion += rowRegion;
        }

        String xmlString = String.format(ROOT_CANVAS,
                                         dump.getDumpReferences(),
                                         dump.getMajorEntity(),
                                         rowsDepartment, rowsEmployee, rowsRegion);
        
        FileWriter xmlFileWriter = new FileWriter(xml);
        xmlFileWriter.write(xmlString);
        xmlFileWriter.close();
    }
}
